package com.ccgk.logistics.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Country;
import com.ccgk.logistics.service.CountryService;

@Controller
@RequestMapping("/country")
public class CountryController {
	
	@Autowired
	private CountryService countryService;
	
	/**查询国家列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		
		CountryService.ContainerCriteria  criteria = new CountryService.ContainerCriteria();
		//查询条件增加，目前不明确
		String name = request.getParameter("name");
		if(StringUtils.isNotBlank(name))
			criteria.nameLike(name);
		PaginationVo<Country> result = countryService.findList(criteria, currentPage, pageSize);
		request.setAttribute("result",result);
		return "country/list";
	}
	
	/**获取单个国家*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model){
		Country country = countryService.getById(id);
		model.addAttribute("obj", country);
		return "country/edit";
	}
	
	/**更新国家信息*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Country country){
		Country _country = countryService.getById(country.getId());
		if(_country != null && !_country.getId().equals(country.getId()))
			return "{flag:1}";
		countryService.update(country);
		return "{flag:0}";
	}
	/**新增国家页*/
	@RequestMapping("/add")
	public String showAdd(){
		return "country/add";
	}
	/**新增国家*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Country country,HttpServletResponse response){
		countryService.add(country);
		return "{flag:0}";
	}
	/**删除国家*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		countryService.batchDelete(idIntArray);
		return "{success:true}";
	}
}
